package main.java.easy;

import java.util.ArrayList;

/**
 * @Author: 郑国荣
 * @Date: 2023-07-21-17:42
 * @Description:
 */
public class PlusOne {
    public static int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] == 9) {
                digits[i] = 0;
            } else {
                digits[i] += 1;
                return digits;
            }
        }
        //如果所有位都是进位，则长度+1
        digits= new int[digits.length + 1];
        digits[0] = 1;
        return digits;
    }






//    太菜了，写了大半天无用功
/*    public static int[] plusOne(int[] digits){
        int n = digits.length-1;
        int index= n;
        int sum=0;
        int count=0;
        boolean digit=true;
        while (index>=0){
            if (digits[index]!=9) digit=false;
            int a = 1;
            for (int i = 0; i < count; i++) {
                a*=10;
            }
            sum+= digits[index]*a;
            index--;
            count++;
        }
        if (digit) {
            count++;
        }
        sum++;
        int[] ints = new int[count];
        while (count>0){
            ints[count-1]=sum%10;
            sum/=10;
            count--;
        }
        return ints;
    }*/

    public static void main(String[] args) {
//      十位数导致整数溢出
        int[] a ={9,8,7,6,5,4,3,0};
        int[] ints =plusOne(a);
        for (int anInt : ints) {
            System.out.print(anInt);
        }
    }
}
